using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CocaCola_Inventory_System.Forms
{
    public partial class Locations : Form
    {
        DataTable data;
        DataTable countries;
        Insert i;
        retrieve r;
        public Locations()
        {
            InitializeComponent();
        }

        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button != MouseButtons.Right)
                return;

            dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
        }

        private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button != MouseButtons.Right)
                return;

            dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            groupBox1.Visible = true;
            groupBox2.Visible = false;
        }

        private void Locations_Load(object sender, EventArgs e)
        {
            if (((CocaCola_Inventory_System.Forms.Container)this.MdiParent).security < 3)
            {
                MessageBox.Show("Not Authorized");
                return;
            }
            r = new retrieve(((CocaCola_Inventory_System.Forms.Container)this.MdiParent).con);
            i = new Insert(((CocaCola_Inventory_System.Forms.Container)this.MdiParent).con);
            retrieve();
            refresh_data();
            comboBox1.DataSource = countries;
            comboBox1.DisplayMember = "name";
            comboBox1.ValueMember = "id";

            comboBox2.DataSource = countries;
            comboBox2.DisplayMember = "name";
            comboBox2.ValueMember = "id";
        }
        public void retrieve()
        {
            if (dataGridView1.SelectedCells.Count < 1)
                return;
            if (dataGridView1.SelectedCells[0].RowIndex < 0 || dataGridView1.SelectedCells[0].RowIndex >= dataGridView1.Rows.Count)
            {

                return;
            }

            textBox2.Text = dataGridView1.SelectedCells[0].OwningRow.Cells[1].Value.ToString();
            textBox4.Text = dataGridView1.SelectedCells[0].OwningRow.Cells[2].Value.ToString();
            textBox5.Text = dataGridView1.SelectedCells[0].OwningRow.Cells[3].Value.ToString();
            int id = Convert.ToInt32(dataGridView1.SelectedCells[0].OwningRow.Cells[5].Value.ToString());
            for (int i = 0; i < countries.Rows.Count; i++)
                if (id == Convert.ToInt32(countries.Rows[i][0]))
                    comboBox2.SelectedIndex = i;
   

        }
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
           
            data = r.getlocations();
            countries = r.getcountries();
            
        }

        public void refresh_data()
        {
            if (!backgroundWorker1.IsBusy)
                backgroundWorker1.RunWorkerAsync();
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {

            dataGridView1.DataSource = data;
            dataGridView1.Columns[0].Visible = false;
            dataGridView1.Columns[5].Visible = false;

            comboBox1.DataSource = countries;
            comboBox1.DisplayMember = "name";
            comboBox1.ValueMember = "id";

            comboBox2.DataSource = countries;
            comboBox2.DisplayMember = "name";
            comboBox2.ValueMember = "id";

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex < 0 || comboBox1.SelectedIndex >= comboBox1.Items.Count)
                return;
            if (i.insertlocation(Convert.ToInt32(countries.Rows[comboBox1.SelectedIndex][0]), textBox1.Text, textBox3.Text, textBox6.Text, ((CocaCola_Inventory_System.Forms.Container)this.MdiParent).id))
            {
                MessageBox.Show("Location Inserted Successfully");
                refresh_data();
            }
            else
                MessageBox.Show("Failed to Insert Location");
        }

        private void editToolStripMenuItem_Click(object sender, EventArgs e)
        {
            groupBox1.Visible = false;
            groupBox2.Visible = true;
            retrieve();
           

        }

        private void button2_Click(object sender, EventArgs e)
        {           
            if (comboBox2.SelectedIndex < 0 || comboBox2.SelectedIndex >= comboBox2.Items.Count)
                return;
            if (i.updatelocation(Convert.ToInt32(dataGridView1.SelectedCells[0].OwningRow.Cells[0].Value), textBox2.Text, textBox4.Text, textBox5.Text, Convert.ToInt32(countries.Rows[comboBox2.SelectedIndex][0]), ((CocaCola_Inventory_System.Forms.Container)this.MdiParent).id))
            {
                MessageBox.Show("Location Successfully Updated");
                refresh_data();
            }
            else
                MessageBox.Show("Location Failed to Update");
            


        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult res = MessageBox.Show("Deleting this Country will result in deleting all its item, Do you want to Procced ?? ", "Confirmation", MessageBoxButtons.YesNo);
            if (res != DialogResult.Yes)
                return;
            if(i.deletelocation(Convert.ToInt32(dataGridView1.SelectedCells[0].OwningRow.Cells[0].Value), ((CocaCola_Inventory_System.Forms.Container)this.MdiParent).id))
            {
                MessageBox.Show("Location Deleted Successfully");
                refresh_data();
            }
            else
                MessageBox.Show("Failed to Delete Location");


        }
    }
}